package com.fcy.teamwork.business.newproject

import android.text.Editable
import android.text.InputFilter
import android.text.Spanned
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.ViewGroup
import com.fcy.teamwork.base.BaseRVAdapter
import com.fcy.teamwork.databinding.ItemNewProjectInfoBinding

class ProjectInfoAdapter : BaseRVAdapter<ProjectInfoAdapter.InfoItem, ItemNewProjectInfoBinding>() {
    data class InfoItem(
        var title: String,
        var limit: Int,
        var content: String = ""
    )

    override fun createBinding(
        inflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ItemNewProjectInfoBinding = ItemNewProjectInfoBinding.inflate(inflater, parent, false)

    override fun bindView(holder: VH, position: Int, data: InfoItem) {
        (holder.binding as? ItemNewProjectInfoBinding)?.apply {
            tvTitle.text = data.title
            etInfo.addTextChangedListener(TextChangerListener(data))
            etInfo.filters = arrayOf(InputFilter.LengthFilter(data.limit))
            tvIndicator.text = data.limit.toString()
        }
    }

    fun getEditedText(): List<InfoItem> {
        return source
    }

    private class TextChangerListener(private val data: InfoItem) : TextWatcher {
        override fun beforeTextChanged(
            s: CharSequence?,
            start: Int,
            count: Int,
            after: Int
        ) {
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            data.content = s.toString()
        }

        override fun afterTextChanged(s: Editable?) {}
    }
}